<template>
  <div>
    <DmbTable
      :columns="columns"
      :reqConfig="reqConfig"
      :saveData="saveData"
      :handleExport="handleExport"
      :delData="del"
      :tools="tools"
    >
    </DmbTable>
  </div>
</template>

<script>
import {
  carrier_EditAPI,
  carrier_AddAPI,
  carrier_DelAPI,
} from "@/api/data/carrier.js";
import { customsClearanceCompany_ListAPI } from "@/api/data/customs-clearance-company.js";
import { destinationMaintenance_ListAPI } from "@/api/data/destination-maintenance.js";
import DmbTable from "@/components/DmbEditTable/index.vue";
export default {
  name: "warehouse-management",
  components: { DmbTable },
  beforeDestroy() {
    this.$store.dispatch("custom_dict/carrier");
  },
  data() {
    return {
      columns: [
        { label: "承运商名称", prop: "carrierName", type: "input" },
        { label: "客户代码", prop: "customCode", type: "input" },
        {
          label: "未使用数量",
          prop: "notUsedNum",
          relues: {
            validate: (data) => {
              const res = /^\d+$/.test(data);
              if (!res) {
                this.$message.error(`未使用数量应为数字`);
              }
              return res;
            },
          },
          type: "input",
        },
        {
          label: "清关公司",
          prop: "customsCompanyId",
          type: "select",
          getOptions: async () => {
            await this.getCustomsClearanceCompany_List();
          },
          options: this.customsClearanceCompany_List,
        },
        // destinationMaintenance_List
        {
          label: "目的地",
          prop: "destinationId",
          type: "select",
          getOptions: async () => {
            await this.getDestinationMaintenance_List();
          },
          options: this.destinationMaintenance_List,
        },
        { label: "是否有效", prop: "isEffective", type: "checkBox" },
        { label: "寄件人id", prop: "senderId", type: "input" },
        { label: "寄件人城市id", prop: "senderCityId", type: "input" },
        { label: "寄件人省份id", prop: "senderProvinceId", type: "input" },
        {
          label: "寄件人手机号",
          prop: "senderPhone",
          relues: {
            validate: (data) => {
              const res = /^1\d{10}$/.test(data);
              if (!res) {
                this.$message.error(`请输入正确格式的手机号`);
              }
              return res;
            },
          },
          type: "input",
        },
        { label: "是否打子单", prop: "isPrint", type: "checkBox" },
        { label: "寄件地址", prop: "senderAddr", type: "input" },
        { label: "快件查询地址", prop: "deliveryQueryAddr", type: "input" },
        { label: "邮编", prop: "zipCode", type: "input" },
        { label: "排序", prop: "sort", type: "input" },
        { label: "ID", prop: "id", disabled: true, type: "input" },
        {
          label: "操作",
          prop: "attribute",
          type: "operate",
          id: "id",
          width: 200,
        },
      ],
      tools: { query: true, export: true, saveApi: true },
      customsClearanceCompany_List: [],
      destinationMaintenance_List: [],
      form: {
        // pageSize: 1000,
        // pageNum: 1,
      },
    };
  },
  computed: {
    reqConfig() {
      return {
        url: "/base/manageCarrier/queryList",
        method: "get",
        params: { ...this.form },
      };
    },
  },
  created() {
    this.getCustomsClearanceCompany_List();
    this.getDestinationMaintenance_List();
  },
  methods: {
    async saveData(data, operate) {
      console.log("保存的数据:", data);
      if (operate) {
        return carrier_EditAPI(data);
      } else {
        return carrier_AddAPI(data);
      }
    },
    async del(id) {
      console.log("删除的数据:", id);
      return carrier_DelAPI(id);
    },
    async getCustomsClearanceCompany_List() {
      // 获取级别列表
      const { rows } = await customsClearanceCompany_ListAPI({
        pageNum: 1,
        pageSize: 999999,
      });
      this.customsClearanceCompany_List = rows.map((el) => {
        return { label: el.customsName, value: el.id };
      });
      this.columns.find((el) => el.label == "清关公司").options =
        this.customsClearanceCompany_List;
    },
    async getDestinationMaintenance_List() {
      // 获取级别列表
      const { rows } = await destinationMaintenance_ListAPI({
        pageNum: 1,
        pageSize: 999999,
      });
      this.destinationMaintenance_List = rows.map((el) => {
        return { label: el.destinationName, value: el.id };
      });
      this.columns.find((el) => el.label == "目的地").options =
        this.destinationMaintenance_List;
    },

    handleExport() {
      this.download(
        "/base/manageCarrier/export",
        {
          /* 参数 */
        },
        "承运商.xls",
        {
          /* 配置项 */
        }
      );
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

